package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;

import com.foolishbird.sdk.params.bean.Popup;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.utils.WL;


public class NoticeTipDialogFragment extends BaseDialogFragment implements View.OnClickListener {


    private WebView mMessageTv;
    private Button mconfirmEvent;

    public static final NoticeTipDialogFragment newInstance(Bundle bundle) {
        NoticeTipDialogFragment fragment = new NoticeTipDialogFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("wan_activity_tip"));
        WL.e("NoticeTipDialogFragment","NoticeTipDialogFragment");
        mMessageTv = contentView.findViewById(getViewIdentifier("activity_tip_message_tv"));
        mconfirmEvent = contentView.findViewById(getViewIdentifier("activity_tip_confirm_btn"));

        mMessageTv.setBackgroundColor(0);
        mMessageTv.getBackground().setAlpha(0);

        mconfirmEvent.setOnClickListener(this);
        Bundle bundle = getArguments();
        if (bundle != null) {
            Popup infoPopup = bundle.getParcelable("infoPopup");
            if (infoPopup != null) {
                loadHtmlContent(infoPopup.getContent());
            } else {
                OnFinishActivityListener listener = getOnFinishActivityListener();
                if (listener != null) {
                    listener.doFinishActivity(this);
                }
            }
        }

        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.gravity = Gravity.CENTER | Gravity.FILL_HORIZONTAL;

        return contentView;
    }

    private void loadHtmlContent(String content) {
        if (mMessageTv != null) {
            mMessageTv.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
        }
    }

    @Override
    public void onClick(View view) {
        OnFinishActivityListener listener = getOnFinishActivityListener();
        if (listener != null) {
            listener.doFinishActivity(this);
        }
    }

    private OnFinishActivityListener getOnFinishActivityListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnFinishActivityListener) {
                return (OnFinishActivityListener) activity;
            }
        }
        return null;
    }
}
